﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Star : MonoBehaviour
{
	public Vector3 point = Vector3.up;
	public int numOfPoints = 10;

	private Mesh mesh;
	private Vector3[] vertices;
	private int[] triangles;


	// Use this for initialization
	void Start ()
	{
		// create a new mesh
		mesh = NewMesh ();
		// update the default mesh, just make it avaiable
		GetComponent<MeshFilter> ().mesh = mesh;
	}

	Mesh NewMesh ()
	{
		// vertices
		vertices = new Vector3[numOfPoints + 1];
		triangles = new int[numOfPoints * 3];

		float angle = -360f / numOfPoints;
		for (int i = 1, t = 0; i<vertices.Length; i++, t+=3) {
			// vertices
			vertices [i] = Quaternion.Euler (0f, 0f, angle * (i - 1)) * point;
			// triangles
			// 0,1,2,0,2,3,0,3,4, ..., 0,10,1
			triangles [t] = 0;
			triangles [t + 1] = i;
			triangles [t + 2] = i + 1;
		}
		triangles [triangles.Length - 1] = 1;

		Mesh mesh = new Mesh ();
		mesh.name = "Star Mesh";
		mesh.vertices = vertices;
		mesh.triangles = triangles;

		return mesh;
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}
}
